package com.sense.kukoo.biz_library.adapter.provider

import android.os.Parcelable
import android.view.View
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.sense.feel.common.utils.ToastUtils
import com.sense.kukoo.biz_library.adapter.FollowAdapter
import com.sense.kukoo.biz_library.adapter.FollowTitleAdapter
import com.sense.kukoo.common.R
import com.sense.kukoo.common.repo.biz.FollowContentBean
import com.sense.kukoo.common.repo.biz.FollowTitleItemBean
import com.sense.kukoo.common.repo.http.bean.UserSearchBean
import com.sense.kukoo.common.utils.ApplicationContext
import com.sense.kukoo.common.wiget.adapter.provider.MultiItemProvider
import com.sense.kukoo.common.wiget.adapter.viewholder.BaseViewHolder
import com.sense.kukoo.lib_logger.LoggerManager

class FollowTitleProvider(
    val listener: FollowAdapter.FollowContentEventListener
): MultiItemProvider<FollowContentBean>() {

    private val TAG: String = javaClass.simpleName

    private var onSaveInstanceState: Parcelable? = null

    override fun getItemType(): Int = FollowContentBean.TYPE_TITLE

    override fun getItemLayoutId(): Int = R.layout.common_item_follow_title

    override fun onBindViewHolder(viewHolder: BaseViewHolder, item: FollowContentBean) {
        val titleList: ArrayList<UserSearchBean> = item.titleList!!
        val currentSelectedUser: UserSearchBean? = item.currentSelectedUser
        //
        //val refreshLayout = viewHolder.getView(R.id.refreshLayout) as SmartRefreshHorizontal
        //
        val tv_recently_updated = viewHolder.getView(R.id.tv_recently_updated) as TextView
        val tv_btn_view_channel = viewHolder.getView(R.id.tv_btn_view_channel) as TextView
        if(currentSelectedUser == null){
            tv_recently_updated.text = ApplicationContext.getString(com.sense.kukoo.common_i18n.R.string.subscriptions_recently_updated)
            tv_btn_view_channel.visibility = View.GONE
        } else {
            tv_recently_updated.text = currentSelectedUser.channelName
            tv_btn_view_channel.visibility = View.VISIBLE
            tv_btn_view_channel.setOnClickListener {
                listener.onTitleChannelClick(item)
            }
        }
        //
        val recyclerView = viewHolder.getView(R.id.recyclerView) as RecyclerView
        if(recyclerView.adapter == null){
            LoggerManager.d(TAG, "recyclerView.adapter == null init")
            recyclerView.adapter = FollowTitleAdapter(mTitleListener, mutableListOf())
            recyclerView.layoutManager = LinearLayoutManager(recyclerView.context, LinearLayoutManager.HORIZONTAL,false)
        }
        //
        val mAdapter = recyclerView.adapter as FollowTitleAdapter
        val list = arrayListOf<FollowTitleItemBean>()
        titleList.forEach {
            list.add(FollowTitleItemBean(user = it, currentSelectedUser = currentSelectedUser))
        }
        mAdapter.list = list
        mAdapter.notifyDataSetChanged()
        //
        onSaveInstanceState?.let {
            LoggerManager.d(TAG, "recyclerView.layoutManager -> onRestoreInstanceState")
            recyclerView.layoutManager?.onRestoreInstanceState(onSaveInstanceState)
        }
    }

    private val mTitleListener = object :FollowTitleAdapter.FollowTitleEventListener{
        override fun onItemClick(user: UserSearchBean) {
            listener.onTitleItemClick(user)
        }
    }

    override fun onViewDetachedFromWindow(holder: BaseViewHolder) {
        super.onViewDetachedFromWindow(holder)
        //
        LoggerManager.d(TAG, "recyclerView.layoutManager -> onSaveInstanceState")
        val recyclerView = holder.getView(R.id.recyclerView) as RecyclerView
        onSaveInstanceState = recyclerView.layoutManager?.onSaveInstanceState()
    }

}